<template>
  <div class="center">
    <!-- 头部 -->
    <div class="table_header flex flex_bet flex-wrap">
      <div class="table_button">
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="Refresh"></el-button>
        <el-button type="primary" size="mini" @click="exporFormVisible = true">导出</el-button>
      </div>
      <div class="table_searchs flex felx_align">
        <div class="table_search flex">
          <el-input placeholder="搜索" v-model="searchTable" class="input-with-select" size="small">
            <el-select v-model="select" slot="prepend" placeholder="请选择" class="table_select" style="width:105px">
              <el-option label="申请开始时间" value="1"></el-option>
              <el-option label="申请结束时间" value="2"></el-option>
              <el-option label="申请用户名" value="3"></el-option>
              <el-option label="审核用户名" value="4"></el-option>
              <el-option label="申请单号" value="5"></el-option>
              <el-option label="返还单号" value="6"></el-option>
              <el-option label="sn码" value="7"></el-option>
            </el-select>
            <el-button slot="append" icon="el-icon-search" @click="idnameSearch"></el-button>
          </el-input>
        </div>
        <dynamicTable :checkboxVal="checkboxVal" @alter="alterCheckbok" />
      </div>
    </div>
    <!-- 专题管理table -->
    <el-table stripe :data="brandTable" style="width: 100%" :key="key" border fit highlight-current-row ref="tableDataRef">
      <el-table-column v-if="colData[0].istrue" prop="apUsername" label="客户" min-width="120" align="center"></el-table-column>
      <el-table-column v-if="colData[1].istrue" prop="order_no" label="申请单号" min-width="160" align="center"></el-table-column>
      <el-table-column v-if="colData[2].istrue" prop="apply_time" label="申请日期" min-width="140" align="center">
        <template slot-scope="scope">{{scope.row.apply_time| newDate}}</template>
      </el-table-column>
      <el-table-column v-if="colData[3].istrue" prop="early_warning" label="售后预警" min-width="100" align="center" sortable>
        <template slot-scope="scope">{{scope.row.early_warning == 1 ? '正常' : scope.row.early_warning == 2 ? '预警' :  '超期'}} </template>
      </el-table-column>
      <el-table-column v-if="colData[4].istrue" prop="product_model" label="产品型号" min-width="160" align="center"></el-table-column>
      <el-table-column v-if="colData[5].istrue" prop="fault_type" label="故障类别" min-width="100" align="center" sortable>
        <template slot-scope="scope">
          {{scope.row.fault_type == 1 ? '显示异常' : scope.row.fault_type == 2 ? '触摸异常' : scope.row.fault_type == 3 ? '充电异常' : scope.row.fault_type == 4 ? '摄像头异常' :
              scope.row.fault_type == 5 ? '喇叭异常' : scope.row.fault_type == 6 ? '耳机异常' : scope.row.fault_type == 7 ? '卡开机画面' : scope.row.fault_type == 8 ? '无法开机' :
              scope.row.fault_type == 9 ? '客户误判' : '其他'}}
        </template>
      </el-table-column>
      <el-table-column v-if="colData[6].istrue" prop="fault_remark" label="故障描述" min-width="170" align="center"></el-table-column>
      <el-table-column v-if="colData[7].istrue" prop="is_analyse" label="是否需分析报告" min-width="120" align="center">
        <template slot-scope="scope">
          {{scope.row.is_analyse == 1 ? '是' : '否'}}
        </template>
      </el-table-column>
      <el-table-column v-if="colData[8].istrue" prop="mail_express" label="寄出快递" min-width="100" align="center" sortable></el-table-column>
      <el-table-column v-if="colData[9].istrue" prop="mail_order_no" label="寄出单号" min-width="160" align="center"></el-table-column>
      <el-table-column v-if="colData[10].istrue" label="受理状态" min-width="100" align="center">
        <template slot-scope="scope">{{scope.row.accept_type == 1 ? '待受理' : scope.row.accept_type == 2 ? '已受理' : '已完成' }}</template>
      </el-table-column>
      <el-table-column v-if="colData[11].istrue" prop="accept_remark" label="受理备注信息" min-width="120" align="center"></el-table-column>
      <el-table-column v-if="colData[12].istrue" label="售后类型" min-width="90" align="center">
        <template slot-scope="scope">{{scope.row.after_type == 1 ? '退货' : scope.row.after_type == 2 ? '换新' : scope.row.after_type == 3 ? '换良品' : scope.row.after_type == 4 ? '保内维修' :
            scope.row.after_type == 5 ? '保外维修' : scope.row.after_type == 6 ? '原反' : '报废' }}</template>
      </el-table-column>
      <el-table-column v-if="colData[13].istrue" label="现象复判" min-width="90" align="center">
        <template slot-scope="scope">{{scope.row.phenomenon == 1 ? '显示异常' : scope.row.phenomenon == 2 ? '触摸异常' : scope.row.phenomenon == 3 ? '充电异常' : scope.row.phenomenon == 4 ? '摄像头异常' :
            scope.row.phenomenon == 5 ? '喇叭异常' : scope.row.phenomenon == 6 ? '耳机异常' : scope.row.phenomenon == 7 ? '开机画面卡' : scope.row.phenomenon == 8 ? '无法开机':
            scope.row.phenomenon == 9 ? '客户误判' : '其他' }}</template>
      </el-table-column>
      <el-table-column v-if="colData[14].istrue" prop="phenomenon_remark" label="现象描述" min-width="140" align="center"></el-table-column>
      <el-table-column v-if="colData[15].istrue" label="分析报告地址" min-width="120" align="center">
        <template slot-scope="scope">
          <div class="flex flex_cen felx_align">
            <i class="iconfont iconanzhuo download" style="border:none;background-color:inherit;color:#a4c872"><a target="_blank" :href="scope.row.analysis_report"></a> </i>
          </div>
        </template>
      </el-table-column>
      <el-table-column v-if="colData[16].istrue" label="处理状态" min-width="100" align="center" sortable>
        <template slot-scope="scope">{{scope.row.handle_type == 1 ? '未收货' : scope.row.handle_type == 2 ? '待处理' : scope.row.handle_type == 3 ? '维修中' : 
            scope.row.handle_type == 4 ? '待返还' : '已返还' }}</template>
      </el-table-column>
      <el-table-column v-if="colData[17].istrue" prop="return_time" label="返还日期" min-width="140" align="center">
        <template slot-scope="scope">{{scope.row.return_time| newDate}}</template>
      </el-table-column>
      <el-table-column v-if="colData[18].istrue" prop="return_express" label="返还快递" min-width="80" align="center"></el-table-column>
      <el-table-column v-if="colData[19].istrue" prop="return_express_order" label="返款单号" min-width="160" align="center"></el-table-column>
      <el-table-column label="操作" min-width="200" align="center">
        <template slot-scope="scope">
          <el-row>
            <el-button type="primary" size="mini" @click="getqueryIdAdminAfterSales(scope.row.id)">售后受理</el-button>
            <el-button size="mini" @click="getqueryReturnAfterSales(scope.row.id)">售后处理</el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="paging">
      <el-pagination :current-page.sync="listQuery.startSize" :page-sizes="[10, 50, 100, 200]" :page-size="listQuery.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total" background @size-change="handleSizeChange" @current-change="handleCurrentChange"></el-pagination>
    </div>

    <!-- 售后受理start -->
    <el-dialog title="售后信息" :visible.sync="addDialog" :close-on-click-modal="false" :close-on-press-escape="false" width="35%">
      <el-form :model="addForm" ref="ruleForm" class="diolog_adapt">
        <el-form-item label="故障类别:" label-width="100px" class="boxitem">
          <el-select placeholder="没有选中任何项" v-model="addForm.faultType">
            <el-option v-for="item in fault" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="处理类别:" label-width="100px" class="boxitem">
          <el-select placeholder="没有选中任何项" v-model="addForm.handleType">
            <el-option v-for="item in typeDate" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="寄出快递:" label-width="100px" prop="mailExpress">
          <el-input v-model="addForm.mailExpress" autocomplete="off" placeholder="请输入快递公司"></el-input>
        </el-form-item>
        <el-form-item label="寄出单号:" label-width="100px" prop="mailOrderNo">
          <el-input v-model="addForm.mailOrderNo" autocomplete="off" placeholder="请输入快递单号"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:right">
        <el-button @click="addDialog = false" size="mini" round>取 消</el-button>
        <el-button type="primary" @click="getacceptAfterSales" size="mini" round>确 定</el-button>
      </div>
    </el-dialog>
    <!-- 售后受理end -->

    <!-- 售后处理start -->
    <el-dialog title="售后处理" :visible.sync="mangeDialog" :close-on-click-modal="false" :close-on-press-escape="false" width="35%">
      <el-form :model="manageForm" ref="manageForm" class="diolog_adapt">
        <el-form-item label="售后联系人:" label-width="100px" prop="mailExpress">
          <el-input v-model="manageForm.afterSales.contact_person" autocomplete="off" placeholder="请输入快递公司"></el-input>
        </el-form-item>
        <el-form-item label="售后手机号码:" label-width="100px" prop="mailOrderNo">
          <el-input v-model="manageForm.afterSales.contact_phone" autocomplete="off" placeholder="请输入快递单号"></el-input>
        </el-form-item>
        <el-form-item label="售后地址:" label-width="100px" prop="mailOrderNo">
          <el-input v-model="manageForm.afterSales.contact_address" autocomplete="off" placeholder="请输入快递单号"></el-input>
        </el-form-item>
        <el-form-item label="故障类别:" label-width="100px" class="boxitem">
          <el-select placeholder="没有选中任何项" v-model="manageForm.afterSales.fault_type">
            <el-option v-for="item in fault" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="受理状态:" label-width="100px" class="boxitem">
          <el-select placeholder="没有选中任何项" v-model="manageForm.afterSales.accept_type">
            <el-option v-for="item in status" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="售后类型:" label-width="100px" class="boxitem">
          <el-select placeholder="没有选中任何项" v-model="manageForm.afterSales.after_type">
            <el-option v-for="item in afterType" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="现象复判:" label-width="100px" class="phenomenon">
          <el-select placeholder="没有选中任何项" v-model="manageForm.afterSales.fault_type">
            <el-option v-for="item in fault" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="处理类别:" label-width="100px" class="boxitem">
          <el-select placeholder="没有选中任何项" v-model="manageForm.afterSales.handle_type">
            <el-option v-for="item in typeDate" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="现象备注:" label-width="100px" prop="mailOrderNo">
          <el-input v-model="manageForm.afterSales.phenomenon_remark" autocomplete="off" placeholder="请输入现象备注"></el-input>
        </el-form-item>
        <el-form-item label="分析报告:" label-width="100px" prop="other_demand">
          <div class="addPut">
            <el-input v-model="manageForm.afterSales.analysis_report" autocomplete="off" placeholder="请直接导入xlsx表格" style="flex:1" disabled></el-input>
            <el-upload class="upload-demo dlePut" style="top: 0;right:10px" accept=".xlsx,.xls" :action="`${uploadLink}api/custom/uploadCustom`" :show-file-list="false" :data="pdfData" :on-success="unloadXlsx">
              <el-button size="mini">上传</el-button>
            </el-upload>
          </div>
        </el-form-item>
        <el-form-item label="返还快递:" label-width="100px" prop="mailOrderNo">
          <el-input v-model="manageForm.afterSales.return_express" autocomplete="off" placeholder="请输入返还快递"></el-input>
        </el-form-item>
        <el-form-item label="返还快递单号:" label-width="100px" prop="mailOrderNo">
          <el-input v-model="manageForm.afterSales.return_express_order" autocomplete="off" placeholder="请输入返还快递单号"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:right">
        <el-button @click="mangeDialog = false" size="mini" round>取 消</el-button>
        <el-button type="primary" @click="getreturnAfterSales" size="mini" round>确 定</el-button>
      </div>
    </el-dialog>
    <!-- 售后处理end -->

    <!-- 售后导出start -->
    <el-dialog title="售后导出" :visible.sync="exporFormVisible" :close-on-click-modal="false" :close-on-press-escape="false" width="40%">
      <el-form :model="exporForm">
        <el-form-item label="导出类型:" label-width="130px">
          <el-radio-group v-model="exporForm.type">
            <el-radio :label="1">导出全部</el-radio>
            <el-radio :label="2">导出待受理</el-radio>
            <el-radio :label="3">导出已受理</el-radio>
            <el-radio :label="4">导出已完成</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="exporFormVisible = false" size="mini" round>取 消</el-button>
        <el-button type="primary" @click="getexportAfterSales" size="mini" round>确 定</el-button>
      </div>
    </el-dialog>
    <!-- 售后导出end -->
  </div>
</template>

<script>
import dynamicTable from "@/components/DynamicTable";
import objectable from "@/utils/objectable.js";
import { queryAdminAfterSales, queryIdAdminAfterSales, acceptAfterSales, queryReturnAfterSales, returnAfterSales, exportAfterSales } from "@/api/aftersale.js";
export default {
  components: { dynamicTable },
  data() {
    return {
      checkboxVal: ['客户', '申请单号', '申请日期', '售后预警', '产品型号', '故障类别', '故障描述', '是否需分析报告', '寄出快递', '寄出单号', '受理状态', '受理备注信息', '售后类型',
        '现象复判', '现象描述', '分析报告', '处理状态', '返还日期', '返还快递', '返款单号'],
      key: 1,
      colData: [],//动态控制table
      brandTable: [],
      select: "", //搜索条件
      searchTable: "",
      total: 0, //总数量
      fault: [{ id: 1, name: '显示异常' }, { id: 2, name: '触摸异常' }, { id: 3, name: '充电异常' }, { id: 4, name: '摄像头异常' }, { id: 5, name: '喇叭异常' }, { id: 6, name: '耳机异常' },
      { id: 7, name: '卡开机画面' }, { id: 8, name: '无法开机' }, { id: 9, name: '客户误判' }, { id: 10, name: '其他' }],
      typeDate: [{ id: 1, name: '未收货' }, { id: 2, name: '已收货' }, { id: 3, name: '维修中' }, { id: 4, name: '待返还' }, { id: 5, name: '已返还' }],
      status: [{ id: 1, name: '待受理' }, { id: 2, name: '已受理' }],
      afterType: [{ id: 1, name: '退货' }, { id: 2, name: '换新' }, { id: 3, name: '换良品' }, { id: 4, name: '保内维修' }, { id: 5, name: '保外维修' }, { id: 6, name: '原返' }, { id: 7, name: '报废' }],
      pdfData: { userId: localStorage.getItem("ID"), last_ip: sessionStorage.getItem("Ip") },
      listQuery: { startSize: 1, pageSize: 10, startTime: undefined, endTime: undefined, apUsername: undefined, aeUsername: undefined, orderNo: undefined, returnOrderNo: undefined, sn: undefined },
      addDialog: false,
      addForm: { faultType: '', mailExpress: '', mailOrderNo: '', handleType: '' },
      addId: 0,
      mangeDialog: false,//售后处理
      manageForm: {
        afterSales: {
          accept_type: '', after_type: '', analysis_report: '', contact_address: '', contact_person: '', contact_phone: '', fault_type: '', handle_type: '', phenomenon: '',
          phenomenon_remark: '', return_express: '', return_express_order: ''
        }
      },
      manageId: 0,
      exporFormVisible: false,
      exporForm: { type: 1, userId: localStorage.getItem("ID") },
      uploadLink: process.env.VUE_APP_BASE_API
    }
  },
  created() {
    this.colData = objectable(this.checkboxVal, this.checkboxVal);//转为数组对象，好控制table
    this.getqueryAdminAfterSales()
  },
  methods: {
    //售后导出
    getexportAfterSales() {
      console.log('参数', this.exporForm)
      exportAfterSales(this.exporForm).then(res => {
        window.open('https://' + res.data.url)
      }).catch(err => {
        err.code == 400 ? this.$message.error(err.msg) : ''
      })
    },
    //获取返回订单号
    getqueryReturnAfterSales(id) {
      this.manageId = id
      queryReturnAfterSales({ id: id }).then(({ data }) => {
        this.mangeDialog = true
        Object.keys(this.manageForm.afterSales).forEach(key => this.manageForm.afterSales[key] = data[key])
      }).catch(err => { })
    },
    //分析报告导入表格
    unloadXlsx(res) {
      if (res.code == 200) {
        this.manageForm.afterSales.analysis_report = res.data.filePath
      } else {
        this.$message.error(res.msg)
      }
    },
    //修改售后返还信息
    getreturnAfterSales() {
      let manageForm = JSON.parse(JSON.stringify(this.manageForm))
      manageForm.afterSales = JSON.stringify(manageForm.afterSales)
      manageForm.userId = localStorage.getItem("ID")
      manageForm.last_ip = sessionStorage.getItem("Ip")
      manageForm.id = this.manageId
      returnAfterSales(manageForm).then(res => {
        this.$message.success(res.msg)
        this.getqueryAdminAfterSales()
        this.mangeDialog = false
      }).catch(err => { })
    },
    //根据ID获取待受理售后信息
    getqueryIdAdminAfterSales(id) {
      this.addId = id
      queryIdAdminAfterSales({ id: id }).then(({ data }) => {
        this.addDialog = true
        this.addForm.faultType = data.fault_type
        this.addForm.mailExpress = data.mail_express
        this.addForm.mailOrderNo = data.mail_order_no
        this.addForm.handleType = data.handle_type
      }).catch(err => { })
    },
    //修改受理售后信息
    getacceptAfterSales() {
      let addForm = JSON.parse(JSON.stringify(this.addForm))
      addForm.id = this.addId
      addForm.userId = localStorage.getItem("ID")
      addForm.last_ip = sessionStorage.getItem("Ip")
      acceptAfterSales(addForm).then(res => {
        this.getqueryAdminAfterSales()
        this.addDialog = false
      }).catch(err => {
        this.getqueryAdminAfterSales()
        this.$message.error(err.msg)
      })
    },
    getqueryAdminAfterSales() {
      queryAdminAfterSales(this.listQuery).then(res => {
        this.brandTable = res.data
        this.total = res.total
      }).catch(err => {
        this.brandTable = []
        this.total = 0
      })
    },
    //分页
    handleSizeChange(val) {
      this.listQuery.pageSize = val
      this.getqueryAdminAfterSales();
    },
    handleCurrentChange(val) {
      this.listQuery.startSize = val
      this.getqueryAdminAfterSales();
    },
    //搜索,1，专题名称
    idnameSearch() {
      if (!this.select) return this.$message.warning("请先选择查询条件");
      this.listQuery.startSize = 1
      this.listQuery.pageSize = 10
      switch (this.select) {
        case '1':
          this.listQuery.startTime = this.searchTable
          break;
        case '2':
          this.listQuery.endTime = this.searchTable
          break;
        case '3':
          this.listQuery.apUsername = this.searchTable
          break;
        case '4':
          this.listQuery.aeUsername = this.searchTable
          break;
        case '5':
          this.listQuery.orderNo = this.searchTable
          break;
        case '6':
          this.listQuery.returnOrderNo = this.searchTable
          break;
        case '7':
          this.listQuery.sn = this.searchTable
          break;
      }
       this.getqueryAdminAfterSales();
    },
    Refresh() {
      this.listQuery = { startSize: 1, pageSize: 10, startTime: undefined, endTime: undefined, apUsername: undefined, aeUsername: undefined, orderNo: undefined, returnOrderNo: undefined, sn: undefined }
      this.getqueryAdminAfterSales()
    },
    //下拉动态table子组件
    alterCheckbok(key, conte) {
      this.key = key;
      this.colData.filter(i => {
        if (conte.indexOf(i.title) != -1) {
          i.istrue = false;
          this.$nextTick(() => {
            this.$refs.tableDataRef.doLayout();
          });
        } else {
          i.istrue = true;
          this.$nextTick(() => {
            this.$refs.tableDataRef.doLayout();
          });
        }
      });
    }
  }
}
</script>

<style scoped>
/deep/ .el-form-item__label {
  font-weight: 500;
}
/deep/ .selectPopper {
  display: none;
}
</style>